package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.nursing.service.IWechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.ReactiveSetCommands;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements IWechatService {



    private static final String OPENID_URL="https://api.weixin.qq.com/sns/jscode2session";

    private static final String ACCESS_TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token";

    private static final String PHONE_URL="https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Value("${wechat.appid}")
    private String appid;

    @Value("${wechat.secret}")
    private String secret;

    @Override
    public String getOpenid(String code) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("appid",appid);
        map.put("secret",secret);
        map.put("js_code",code);
        map.put("grant_type","authorization_code");
        String result = HttpUtil.get(OPENID_URL,map);
        /*将HTTP请求返回的JSON字符串结果解析为JSONObject对象，便于后续从中提取openid等字段信息。*/
        JSONObject entries = JSONUtil.parseObj(result);
        if (entries.get("errcode")==null || entries.getInt("errcode")==0){
            String openid = entries.getStr("openid");
            return openid;
        }else {
            throw  new RuntimeException("获取openid失败"+entries.toJSONString(2));
        }

    }

    @Override
    public String getPhone(String detailCode) {
        String token=getAccessToken();
        String url=PHONE_URL+token;
        HashMap<String,Object> map=new HashMap<>();
        map.put("code",detailCode);
        String result=HttpUtil.post(url,JSONUtil.toJsonStr(map));
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if (jsonObject.getInt("errcode")==null || jsonObject.getInt("errcode")==0){
            JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
            String phoneNumber = phoneInfo.getStr("phoneNumber");
            return phoneNumber;
        }else {
            throw new RuntimeException("获取手机号失败"+jsonObject.toJSONString(2));
        }



    }
    @Override
    public String getAccessToken(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("grant_type","client_credential");
        map.put("appid",appid);
        map.put("secret",secret);
        String result = HttpUtil.get(ACCESS_TOKEN_URL, map);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if (jsonObject.get("errcode")==null || jsonObject.getInt("errcode")==0){
            String token = jsonObject.getStr("access_token");
            return token;
        }else {
        throw  new RuntimeException("获取token失败"+jsonObject.toJSONString(2));
        }
    }
}
